package cn.example.config;

import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;

import javax.sql.DataSource;

/*@Configuration*/
public class SqlSessionFactoryConfig {

    /*@Autowired*/
    private DataSource dataSource;

    /*@Bean*/
    public SqlSessionFactory sqlSessionFactory() throws Exception{
        try{
            SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
            sqlSessionFactoryBean.setDataSource(dataSource);
            sqlSessionFactoryBean.setTypeAliasesPackage("cn.example.pojo");

            sqlSessionFactoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/*.xml"));

            return sqlSessionFactoryBean.getObject();
        }catch(Exception e){
            e.printStackTrace();
            throw e;
        }
    }
}
